﻿$(function () {
    var url = new Url();
    var $Id = url.GetQueryString("id");
    var $css = url.GetQueryString("css");
    //修改时绑定页面
    if ($Id && !isNaN($Id)) {
        $.ajax({
            type: "get",
            url: "../Handler/MenuHandler.ashx",
            dataType: "json",
            data: { Id: $Id, type: 4 },
            async: false,
            cache: false,
            success: function (data) {
                $(".txtName").val(data.MenuName);
                $(".txtUrl").val(data.url);
                $(".ddlPMenu option[value='" + $Id + "']").remove();   //删除Select中Value='3'的Option
                $(".ddlPMenu").val(data.ParentMenuId);
            }
        });
    }
    var serializer = new JSONSerializer();
    //保存
    $(".btnSave").bind("click", function () {
        var $formData = serializer.Serialize($("#formArea")[0]);
        if (!$formData.Name) {
            $(".txtName").focus();
            $(".errorName").text("菜单不能为空");
            return;
        }
        $formData.type = $Id && !isNaN($Id) ? 2 : 1;
        if ($Id && !isNaN($Id)) {
            $formData.Id = $Id;
        }
        $formData._=Math.random();
        $.getJSON("../Handler/MenuHandler.ashx", $formData,
            function (result) {
                alert(result.Message);
                if (result.Status) {
                    location.href = "MenuList.aspx?css="+$css;
                }
            }
        );
    });

    $(".txtName").change(function () {
        $(".errorName").text("");
    });
});